﻿using Shop.Core.Repository;
using Shop.Core.Share;
using Shop.IServices.Admin;
using Shop.Model.Dto.Admin.System;
using Shop.Model.Entity.System;
using Shop.Model.View.System;

namespace Shop.Services.Admin;

public class SysConstantService: AbstractService, ISysConstantService
{
    private readonly RepositoryBase<SysConstantEntity> _constantRepository;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="constantRepository"></param>
    public SysConstantService(
        RepositoryBase<SysConstantEntity> constantRepository)
    {
        _constantRepository = constantRepository;
    }
    
    /// <summary>
    /// 系统配置查询
    /// </summary>
    /// <returns></returns>
    /// <exception cref="NotImplementedException"></exception>
    public AppResult<List<ConstantListVo>> ConstantList()
    {
        var data = _constantRepository.Where(x => x.Group == "系统配置")
            .OrderBy(x => x.Sort)
            .Select<ConstantListVo>()
            .ToList();
        foreach (var item in data)
        {
            item.Value = item.Key switch
            {
                "drawings_procedure" => $"{(int)(decimal.Parse(item.Value) * 100)}%",
                "drawings_low" => $"{item.Value}元",
                "drawings_wechat_high" => $"{item.Value}元",
                "drawings_clip_high" => $"{item.Value}元",
                "drawings_wechat_isOpen" => bool.Parse(item.Value) ? "已开启" : "未开启",
                "drawings_clip_isOpen" => bool.Parse(item.Value) ? "已开启" : "未开启",
                _ => item.Value
            };
        }
        return data;
    }

    /// <summary>
    /// 修改系统常量
    /// </summary>
    /// <param name="dto"></param>
    /// <param name="userContextId"></param>
    /// <returns></returns>
    /// <exception cref="NotImplementedException"></exception>
    public AppResult UpdateConstant(UpdateConstantDto dto, long userContextId)
    {
        var data = _constantRepository.Where(x => x.Group == "系统配置" && x.Key == dto.Key)
            .First();
        if (data == null) return Problem("系统配置系数不存在");
        switch (dto.Key)
        {
            case "drawings_procedure":
                var isNum = int.TryParse(dto.Value, out var num);
                if (!isNum) return Problem("请输入数值");
                if (num is < 0 or > 100) return Problem("手续费区间为0-100，请规范输入");
                var deNum = num / 100M;
                data.Value = deNum.ToString();
                _constantRepository.Update(data);
                return new AppResult();
            case "drawings_low":
                var isNumLow = int.TryParse(dto.Value, out var numLow);
                if (!isNumLow) return Problem("请输入数值");
                if (numLow < 0) return Problem("起提金额不可小于0");
                data.Value = dto.Value;
                _constantRepository.Update(data);
                return new AppResult();
            case "drawings_wechat_high":
                var isNumWecHigh = int.TryParse(dto.Value, out var numWecHigh);
                if (!isNumWecHigh) return Problem("请输入数值");
                if (numWecHigh < 0) return Problem("微信最高金额不可小于0");
                data.Value = dto.Value;
                _constantRepository.Update(data);
                return new AppResult();
            case "drawings_clip_high":
                var isNumClipHigh = int.TryParse(dto.Value, out var numClipHigh);
                if (!isNumClipHigh) return Problem("请输入数值");
                if (numClipHigh < 0) return Problem("银行卡最高金额不可小于0");
                data.Value = dto.Value;
                _constantRepository.Update(data);
                return new AppResult();
            case "drawings_wechat_isOpen":
                var isDrawWecIsOpen = int.TryParse(dto.Value, out var drawWecIsOpen);
                if (!isDrawWecIsOpen) return Problem("选择错误");
                if (drawWecIsOpen != 0 && drawWecIsOpen != 1) return Problem("请选择选项");
                data.Value = drawWecIsOpen == 1 ? "true" : "false";
                _constantRepository.Update(data);
                return new AppResult();
            case "drawings_clip_isOpen":
                var isDrawClipIsOpen = int.TryParse(dto.Value, out var drawClipIsOpen);
                if (!isDrawClipIsOpen) return Problem("选择错误");
                if (drawClipIsOpen != 0 && drawClipIsOpen != 1) return Problem("请选择选项");
                data.Value = drawClipIsOpen == 1 ? "true" : "false";
                _constantRepository.Update(data);
                return new AppResult();
            default:
                return Problem("系统配置系数不存在");
        }
    }
}